--created by seiteki a long time ago, modified by looter so it would actually work.
--removed offensive comments, some people are hurt by the internets serious business :(
--Those comments have been there since May 2007, anyway!


scnr = { }
scnr.plyr = { }
function scnr.Init( pl )
if( !pl:IsCombine() ) then return false; end

	local already = false
	for k, v in pairs( scnr.plyr ) do
		if v[1] == pl then
			already = true
		end
	end
	
	if !already then
		s = ents.Create( "npc_cscanner" )
		s:SetPos( pl:GetPos() )
		s:SetAngles( pl:GetAngles() )
		s:SetKeyValue( "SetDistanceOverride", "64" )
		s:SetKeyValue( "spawnflags" , "272" )
		s:SetKeyValue( "renderfx", "0" )
		s:SetKeyValue( "ShouldInspect", "1" )
		s:SetKeyValue( "spotlightlength", "3" )
		s:SetKeyValue( "spotlightwidth", "3" )
		
		t = ents.Create( "path_corner" )
		t:SetPos( pl:GetPos() )
		t:SetAngles( pl:GetAngles() )
		t:Spawn()
		t:SetName( "scanner_target_" .. string.gsub( tostring( s:GetTable() ), "table: ", "" ) )
		
		s:SetKeyValue( "target", t:GetName() )
		s:Fire( "clearfollowtarget", "", 0 )
		s:Fire( "setfollowtarget", "scanner_target_" .. string.gsub( tostring( s:GetTable() ), "table: ", "" ), 0 )
		
		s:Spawn()
		
		pl:StripWeapons()
		pl:Spectate( OBS_MODE_CHASE ) 
		pl:SpectateEntity( s )
		table.insert( scnr.plyr, { pl, s, t } )
	end
end
concommand.Add( "eng_scanner", scnr.Init )

local LastFlash = 0;
local LastWarn = 0;

function scnr.Think()
	for k, v in pairs( scnr.plyr ) do
		local pl = v[1]
		local s = v[2]
		local t = v[3]
		if pl:IsValid() and s:IsValid() and t:IsValid() then
			if pl:KeyDown( IN_FORWARD ) then
				local ang = pl:GetAimVector( )
				local trace = { }
				trace.start = s:GetPos()
				trace.endpos = trace.start + ( ang * 40960 )
				trace.filter = s
				local tr = util.TraceLine( trace )
				if tr.Hit then
					t:SetPos( tr.HitPos )
					pl:SetPos( tr.HitPos )
					s:Fire( "clearfollowtarget", "", 0 )
					s:Fire( "setfollowtarget", "scanner_target_" .. string.gsub( tostring( s:GetTable() ), "table: ", "" ), 0 )
				end
			if pl:KeyDown( IN_ATTACK ) then
				if( CurTime() - LastFlash > 5 ) then
					LastFlash = CurTime();
					pl:EmitSound( "NPC_CScanner.TakePhoto" )
					--pl:AttemptFlash();
				end
			end
			--elseif pl:KeyDown( IN_ATTACK ) then
				--if( CurTime() - LastWarn > 3 ) then
					--LastWarn = CurTime();
					--pl:EmitSound( "npc/scanner/scanner_siren2.wav" )
				--end
			end
		end
	end
end
hook.Add( "Think", "scnrThink", scnr.Think )

function scnr.Close( pl )

	for k, v in pairs( scnr.plyr ) do
	
		if v[1] == pl then
			pl:UnSpectate()
			v[2]:Fire( "break", "", 0 )
			v[2]:Remove()
			v[3]:Remove()
			pl:Spawn();
			table.remove( scnr.plyr, k )
		end
		
	end
	


end
concommand.Add( "eng_stopscanner", scnr.Close )

function scnr.Killed( victim, killer, wep )

	for k, v in pairs( scnr.plyr ) do
	
		if v[2] == victim then
			v[1]:UnSpectate()
			v[3]:Remove()
			v[1]:Spawn();
			table.remove( scnr.plyr, k )
		end
		
	end
	
end
hook.Add( "OnNPCKilled", "scnrKilled", scnr.Killed )

function scnr.PlayerGone( victim, weapon, killer )

	for k, v in pairs( scnr.plyr ) do
	
		if v[1] == victim then
			v[1]:UnSpectate()
			v[2]:Remove()
			v[3]:Remove()
			v[1]:Spawn()
			table.remove( scnr.plyr, k )
		end
		
	end
	
end
hook.Add( "OnPlayerDeath", "scnrPlayerKilled", scnr.PlayerGone )
hook.Add( "OnPlayerDisconnected", "scnrPlayerDisconnected", scnr.PlayerGone )